package com.phpxiu.app.model.list;

import com.phpxiu.app.kkylive.KKYApp;
import com.phpxiu.app.kkylive.R;
import com.phpxiu.app.model.MsgSender;
import com.tencent.TIMConversation;
import com.tencent.TIMFaceElem;
import com.tencent.TIMMessage;
import com.tencent.TIMTextElem;

import java.nio.charset.Charset;

/**
 * 会话数据
 */
public class C2CConversation extends ListItem{
    private final static String VOICE_MSG= KKYApp.getInstance().getString(R.string.msg_voice);
    private TIMConversation conversation;
    private TIMMessage lastMessage;
    private String peer;
    private MsgSender mSender;
    public C2CConversation(TIMConversation conversation,MsgSender sender){
        this.conversation = conversation;
        conversation.getLastMsgs(1);
        this.peer=conversation.getPeer();
        mSender=sender;
    }

    public TIMConversation getConversation() {
        return conversation;
    }

    /**
     * 获取会话id
     * @return
     */
    public String getPeer() {
        return peer;
    }

    /**
     *  设置最新消息
     * @param lastMessage
     */
    public void setLastMessage(TIMMessage lastMessage) {
        this.lastMessage = lastMessage;
    }

    /**
     * 获取会话对方资料
     * @return mSender
     */
    public MsgSender getSender(){
        return mSender;
    }

    /**
     * 设置会话对话信息
     * @param sender
     */
    public void setSender(MsgSender sender){
        this.mSender=sender;
    }

    /**
     * 获取会话对方昵称
     * @return
     */
    public String getNick(){
        if(mSender==null){
            return "";
        }else{
            return mSender.getNickname();
        }
    }

    /**
     * 获取会话对方头像url
     */
    public String getAvatar() {
        if(mSender==null){
            return "";
        }else{
            return mSender.getAvatar();
        }
    }

    /**
     * 获取会话对方性别
     */
    public String getSex(){
        if(mSender==null){
            return "1";
        }else{
            return mSender.getSex();
        }
    }

    /**
     * 获取会话对方等级
     * @return
     */
    public int getLv(){
        if(mSender==null){
            return 1;
        }else{
            return mSender.getLvl();
        }
    }

    public TIMMessage getLastMessage() {
        return lastMessage;
    }

    /**
     * 获取最后一条消息摘要
     */
    public String getLastMessageSummary() {
        if(lastMessage==null||lastMessage.getElementCount()==0){
            return null;
        }
        switch (lastMessage.getElement(0).getType()){
            case Text:
            case Face:
                return getTextSummary(lastMessage);
            case Sound:
                return KKYApp.getInstance().getString(R.string.msg_voice);
            case Image:
                return KKYApp.getInstance().getString(R.string.msg_pic);
            case Custom:
                //conversation.setReadMessage(lastMessage);
                //return KKYApp.getInstance().getString(R.string.c2c_chat_picture_summary);
            default:
                return "";
        }
    }

    /**
     * 获取最后一条消息的时间
     */
    public long getLastMessageTime() {
        if (lastMessage == null) {
            return 0;
        }
        return lastMessage.timestamp();
    }

    /**
     * 获取未读消息数量
     */
    public long getUnReadNum() {
        if (conversation == null) {
            return 0;
        }
        return conversation.getUnreadMessageNum();
    }

    /**
     * 将所有消息标记为已读
     */
    public void readAllMessage() {
        if (conversation != null){
            conversation.setReadMessage();
        }
    }

    /**
     * 获取消息摘要
     */
    public static String getTextSummary(TIMMessage message) {
        StringBuilder result = new StringBuilder();
        for (int i = 0; i<message.getElementCount(); ++i){
            switch (message.getElement(i).getType()){
                case Face:
                    TIMFaceElem faceElem = (TIMFaceElem) message.getElement(i);
                    byte[] data = faceElem.getData();
                    if (data != null){
                        result.append(new String(data, Charset.forName("UTF-8")));
                    }
                    break;
                case Text:
                    TIMTextElem textElem = (TIMTextElem) message.getElement(i);
                    result.append(textElem.getText());
                    break;
            }

        }
        return result.toString();
    }
}
